package com.wbcs.system.datatype;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wbcs.config.database.type.AbsDatabaseType;

public class BlobType extends AbsDataType
{
    private final static Log log=LogFactory.getLog(BlobType.class);

    public Object getColumnValue(ResultSet rs,String column,AbsDatabaseType dbtype)
            throws SQLException
    {
        return dbtype.getBlobValue(rs,column);
    }

    public Object getColumnValue(ResultSet rs,int iindex,AbsDatabaseType dbtype)
            throws SQLException
    {
        return dbtype.getBlobValue(rs,iindex);
    }

    public void setPreparedStatementValue(int iindex,String value,PreparedStatement pstmt,
            AbsDatabaseType dbtype) throws SQLException
    {
        log.debug("setBlob("+iindex+","+value+")");
        dbtype.setBlobValue(iindex,(byte[])label2value(value),pstmt);
    }

    public Object label2value(String label)
    {
        if(label==null) return null;
        return label.getBytes();
    }

    @Override
	public String value2label(Object value)
    {
        if(value==null) return "";
        if(!(value instanceof byte[]))
        {
            return String.valueOf(value);
        }
        return new String((byte[])value);
    }

    public Class getJavaTypeClass()
    {
        return byte[].class;
    }
}
